import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Headers, Http, RequestOptions} from '@angular/http';
import {SoundFile} from '../entity/sound-file.model'
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class AlarmSoundService {
  private readonly param = 'sysSoundFiles';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  /**
   * 获取报警声音
   *
   * @returns {Promise<FaultLevel[]>}
   * @memberof FaultLevelService
   */
  getAlarmSounds(): Promise<SoundFile[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as SoundFile[])
      .catch(this.handleError);
  }

  create(soundFile: SoundFile): Promise<boolean> {
    return this.http.post(this.url, JSON.stringify(soundFile))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return result.data as boolean;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  update(soundFile: SoundFile): Promise<boolean> {
    //const url = `${this.url}`;
    //let url = `http://192.168.15.150:8585/webapi/v1/factorys`
    return this.http.put(this.url, JSON.stringify(soundFile))
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return result;
            case '-1':
              throw new Error(result.message);
            case '8001':
              return result;
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  upload(file: File): Promise<string> {
    let formData = new FormData();
    formData.append('file', file, file.name);
    return this.http.post(`${this.url}/uploadFiles`, formData)
      .toPromise()
      .then(res => {
        try {
          let result = res;
          switch (result.code) {
            case '200':
              return result.data as string;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  //删除
  delete(id: number): Promise<void> {
    const url = `${this.url}/${id}`;
    return this.http.delete(url)
      .toPromise()
      .then(() => null)
      .catch(this.handleError);
  }

  //批量线路删除
  batchDelete(keyIds: number[]): Promise<boolean> {
    const apiUrl = `${this.url}?items=${keyIds}`;
    //const apiUrl = `http://192.168.15.3:8585/webapi/v1/matrixStateGroups?items=${keyIds}`;

    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response;
          switch (result.code) {
            case '200':
              return true;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}
